package com.heima.article.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.article.entity.ApAuthor;
import com.heima.article.mapper.ApAuthorMapper;
import com.heima.article.service.ApAuthorService;
import com.heima.common.enums.AppHttpCodeEnum;
import com.heima.common.exception.LeadException;
import com.heima.common.util.BeanHelper;
import com.heima.model.article.dtos.ArticleDto;
import com.heima.model.article.dtos.AuthorDto;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * <p>
 * APP文章作者信息表 服务实现类
 * </p>
 *
 * @author HM
 * @since 2023-03-22
 */
@Service
public class ApAuthorServiceImpl extends ServiceImpl<ApAuthorMapper, ApAuthor> implements ApAuthorService {

    @Override
    public Integer createAuthor(AuthorDto dto) {
        LambdaQueryWrapper<ApAuthor> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ApAuthor::getWmUserId, dto.getWmUserId());
        ApAuthor authorDB = this.getOne(queryWrapper);
        if (authorDB == null) {
            // insert
            ApAuthor insertAuthor = buildInsertAuthor(dto);
            this.save(insertAuthor);
            return insertAuthor.getId();
        } else {
            // update
            ApAuthor updateAuthor = buildUpdateAuthor(authorDB, dto);
            this.updateById(updateAuthor);
            return updateAuthor.getId();
        }
    }

    /**
     * 根据自媒体用户ID，查询作者信息
     *
     * @param wmUserId 自媒体用户ID
     */
    @Override
    public ApAuthor getByUserId(Integer wmUserId) {
        LambdaQueryWrapper<ApAuthor> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ApAuthor::getWmUserId, wmUserId);
        ApAuthor apAuthorDB = this.getOne(queryWrapper);
        if (apAuthorDB == null) {
            throw new LeadException(AppHttpCodeEnum.AP_AUTHOR_DATA_NOT_EXIST);
        }
        return apAuthorDB;
    }

    /**
     * feign远程接口调用
     *
     * @param userId
     * @return
     */
    @Override
    public ApAuthor getAuthorByUserId(Integer userId) {
        LambdaQueryWrapper<ApAuthor> queryWrapper = new LambdaQueryWrapper<>();
        if (userId == null) {
            throw new LeadException(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        queryWrapper.eq(ApAuthor::getWmUserId, userId);
        ApAuthor author = getOne(queryWrapper);
        return author;
    }


    private ApAuthor buildInsertAuthor(AuthorDto dto) {
        ApAuthor author = BeanHelper.copyProperties(dto, ApAuthor.class);
        author.setCreatedTime(new Date());
        return author;
    }


    private ApAuthor buildUpdateAuthor(ApAuthor authorDB, AuthorDto dto) {
        ApAuthor author = BeanHelper.copyProperties(dto, ApAuthor.class);
        author.setId(authorDB.getId());
        author.setWmUserId(null);
        return author;
    }

    @Override
    public AuthorDto findById(Long id) {
        ApAuthor author = this.getById(id);
        AuthorDto authorDto = BeanHelper.copyProperties(author, AuthorDto.class);
        return authorDto;
    }
}
